const { check, validationResult } = require('express-validator');

const rules = (arr) => {
    return arr.map(item => {
        return check(item.name).notEmpty().withMessage(item.message)
    })
}

exports.rules = rules

const useValid = async (req, res, next) => {
    try {
        const errs = validationResult(req)
        if (!errs.isEmpty()) {
            return res.status(200).send({
                code: 422,
                message: "确少必填项",
                data: errs.array()
            })
        }
        return next();
    }catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
}

exports.valid = useValid

